package com.xzmzhp.controller.cosmetic;

import com.github.pagehelper.PageHelper;
import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.pojo.cosmetic.Cosmetic;
import com.xzmzhp.pojo.cosmetic.CosmeticVO;
import com.xzmzhp.service.cosmetic.CosmeticService;
import com.xzmzhp.utils.return_tool.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 宋雪
 * @Date: 2020/11/19 20:42
 * @Description: 化妆品信息类
 */
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/cosmetic")
public class CosmeticController {
    @Autowired
    private CosmeticService cosmeticService;

    /**
     * 添加化妆品
     *
     * @param cosmetic 化妆品信息
     * @return 是否成功
     */
    @RequestMapping("/addCosmetic")
    @ResponseBody
    public ReturnData addCosmetic(@RequestBody Cosmetic cosmetic) {
        return cosmeticService.addCosmetic(cosmetic);
    }

    /**
     * 根据id删除化妆品
     *
     * @param cosmeticId 化妆品id
     * @return 是否成功
     */
    @RequestMapping("/deleteCosmetic")
    @ResponseBody
    public ReturnData deleteCosmetic(Integer cosmeticId) {
        return cosmeticService.deleteCosmetic(cosmeticId);
    }

    /**
     * 修改化妆品信息
     *
     * @param cosmetic 化妆品信息
     * @return 是否成功
     */
    @RequestMapping("/updateCosmetic")
    @ResponseBody
    public ReturnData updateCosmetic(@RequestBody Cosmetic cosmetic) {
        return cosmeticService.updateCosmetic(cosmetic);
    }

    /**
     * 查询所有化妆品信息
     *
     * @return 化妆品信息
     */
    @RequestMapping("/selectCosmeticAll")
    @ResponseBody
    public ReturnData selectCosmeticAll(@RequestParam(value = "page", defaultValue = "1") int page,
                                        @RequestParam(value = "limit", defaultValue = "10") int limit) {
        PageHelper.startPage(page, limit);
        return cosmeticService.selectCosmeticAll();
    }

    /**
     * 根据id返回化妆品信息
     *
     * @param cosmeticId 化妆品id
     * @return 相应的化妆品信息
     */
    @RequestMapping("/selectCosmeticById")
    @ResponseBody
    public ReturnData selectCosmeticById(Integer cosmeticId) {
        return cosmeticService.selectCosmeticById(cosmeticId);
    }

    /**
     * 根据条件查询化妆品信息
     *
     * @param cosmeticTypeId 化妆品类型id
     * @param cosmeticName   化妆品名称
     * @param maxPrice       价格上限
     * @param minPrice       价格下限
     * @param remark1        化妆品状态
     * @return
     */
    @RequestMapping("/selectCosmeticByParam")
    @ResponseBody
    public ReturnData selectCosmeticByParam(@RequestParam(value = "cosmeticTypeId", defaultValue = "0") Integer cosmeticTypeId,
                                            @RequestParam(value = "cosmeticName", defaultValue = "") String cosmeticName,
                                            @RequestParam(value = "page", defaultValue = "0") BigDecimal maxPrice,
                                            @RequestParam(value = "maxPrice", defaultValue = "0") BigDecimal minPrice,
                                            @RequestParam(value = "remark1", defaultValue = "") String remark1,
                                            @RequestParam(value = "page", defaultValue = "1") int page,
                                            @RequestParam(value = "limit", defaultValue = "10") int limit) {
        PageHelper.startPage(page, limit);
        List<CosmeticVO> list = cosmeticService.selectCosmeticByParam(cosmeticTypeId, cosmeticName, maxPrice, minPrice, remark1);
        if (list != null) {
            return ReturnData.ok(list);
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "没有找到该化妆品");
        }
    }


}
